package net.liteheaven.xblib.common.rom;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ApkUtil {

    /**
     * 按包名获取信息
     * @param ctx
     * @param apk
     * @return
     */
    public static PackageInfo getPackageInfo(Context ctx, String apk){
        PackageInfo pi = null;

        if (!TextUtils.isEmpty(apk)){
            PackageManager pm = ctx.getPackageManager();
            List<PackageInfo> packs = pm.getInstalledPackages(0);
            for (int i = 0; i < packs.size(); i++) {
                PackageInfo p = packs.get(i);
                if (p.versionName == null) {
                    continue;
                }
                ApplicationInfo appInfo = p.applicationInfo;
                if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
                    // 系统程序
                } else {
                    if (apk.equals(p.packageName)){
                        pi = p;
                        break;
                    }
                }
            }
        }

        return pi;
    }

    public static StringBuffer getPackageInfoReport(PackageInfo pi){
        StringBuffer sb = new StringBuffer();
        sb.append("\npackageName="+pi.packageName);
        sb.append("\nversionCode="+pi.versionCode);
        sb.append("\nversionName="+pi.versionName);
        //sb.append("\ninstallLocation="+pi.installLocation);

        return sb;
    }

    /**
     * 获取顶部APK
     * @param ctx
     * @return
     */
    public static String getTopApk(Context ctx){
        String top = "";
        ActivityManager am = (ActivityManager)ctx.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        if (!taskInfo.isEmpty()) {
            ActivityManager.RunningTaskInfo rti = taskInfo.get(0);
            ComponentName topActivity = rti.topActivity;
            top = topActivity.getPackageName();
        }
        if (top == null){
            top = "";
        }
        return top;
    }

    /**
     * 通过apk获取进程ID
     * @param apk
     * @return -1失败
     */
    public static int getProcessId(Context ctx, String apk){
        ActivityManager am = (ActivityManager)ctx.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> list = am.getRunningAppProcesses();

        int pid = -1;
        for (int i = 0; i<list.size(); ++i){
            RunningAppProcessInfo pi = list.get(i);
            Set<String> set = new HashSet<String>();
            set.addAll(Arrays.asList(pi.pkgList));
            if (set.contains(apk)){
                pid = pi.pid;
                break;
            }
        }
        return pid;
    }
}
